#!/bin/bash
long_k8s_version={{ kubernetes_version }}
short_k8s_version=${long_k8s_version#*v}
current_k8s_server_version=`kubectl version |grep "Server Version" |awk '{print $5}' |awk -F':' '{print $2}' |awk -F'"' '{print $2}'`
current_k8s_client_version=`kubectl version |grep "Client Version" |awk '{print $5}' |awk -F':' '{print $2}' |awk -F'"' '{print $2}'`
function version_gt() { test "$(echo "$@" | tr " " "\n" | sort -V | head -n 1)" != "$1"; }
function version_lt() { test "$(echo "$@" | tr " " "\n" | sort -V -r | head -n 1)" != "$1"; }
if (version_lt ${current_k8s_server_version} ${long_k8s_version}) || (version_lt ${current_k8s_client_version} ${long_k8s_version}); then
  yum clean all
  yum install -y --disablerepo=* --enablerepo=wise2c kubeadm-${short_k8s_version}-0 --disableexcludes=kubernetes
  kubectl drain `hostname` --ignore-daemonsets
  #kubeadm upgrade node config --kubelet-version ${long_k8s_version} --certificate-renewal=false
  kubeadm upgrade node --certificate-renewal=false -y
  yum install -y --disablerepo=* --enablerepo=wise2c kubelet-${short_k8s_version}-0 kubectl-${short_k8s_version}-0 --disableexcludes=kubernetes
  systemctl daemon-reload
  systemctl restart kubelet
  kubectl uncordon `hostname`
  rm -f $HOME/.kube/config
else
  echo "current version is not lower than ${long_k8s_version}"
fi
